
clear
set more off
use "$base_out/DWMS_Regresiones.dta", clear


***Elimino secundarias***

drop if secondary==1
***************************************************
***************************************************
*****Regresiones básicas indice y componentes******
***************************************************
***************************************************

*
********************
**PEC PLUS vs. PEC**
eststo clear
label var PEC_PLUS " "
label var PEC "Grants"
eststo:  reghdfe management_sted3 PEC_PLUS   if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2gc")
local tempm_effect=tempm[1,1]*0.23
local tempm_effect=string(`tempm_effect', "%9.2gc")


file open newfile using "$tables/index_sted3_coef_PEC_PLUS_vs_PEC.tex", write replace
file write newfile "`tempm'"
file close newfile

file open newfile using "$tables/index_sted3_coef_PEC_PLUS_vs_PEC_bloom.tex", write replace
file write newfile "`tempm_effect'"
file close newfile


test PEC_PLUS
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/index_sted3_pvalue_PEC_PLUS_vs_PEC.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.3fc")
	file open newfile using "$tables/index_sted3_pvalue_PEC_PLUS_vs_PEC.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
		

		
foreach x in  operations_sted3 monitoring_sted3 targets_sted3 people_sted3 leader_pca3 {
	eststo:  reghdfe `x' PEC_PLUS   if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)

	matrix tempm=e(b)
	local tempm=string(tempm[1,1], "%9.2gc")
	local tempm_effect=tempm[1,1]

	file open newfile using "$tables/`x'_coef_PEC_PLUS_vs_PEC.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	
	test PEC_PLUS
	if r(p)<0.001 {
		di "peque"
		local tempm ="$<0.001$"
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	if r(p)>0.001 {
		di "grande"
		local tempm=string(r(p), "%9.2gc")
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	
	
}


esttab using "$tables/General_PEC_PLUSESTRA.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(PEC_PLUS) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	
		
	**** For each state ****
	
	foreach sta in 1 3 4 5 6 7 9{	
	
	eststo clear
	label var PEC_PLUS "Grants + management"
	label var PEC "Grants"
	eststo:  reghdfe management_sted3 PEC_PLUS   if treatgroup!="Control" & state==`sta', a(estrato#enumerador)  vce(cluster school_id)
	matrix tempm=e(b)
	local tempm=string(tempm[1,1], "%9.2gc")
	local tempm_effect=tempm[1,1]

	file open newfile using "$tables/index_sted3_coef_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	test PEC_PLUS
	if r(p)<0.001 {
		di "peque"
		local tempm ="$<0.001$"
		file open newfile using "$tables/index_sted3_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	if r(p)>0.001 {
		di "grande"
		local tempm=string(r(p), "%9.3fc")
		file open newfile using "$tables/index_sted3_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	
	foreach x in operations_sted3 monitoring_sted3 targets_sted3 people_sted3 {
	eststo:  reghdfe `x' PEC_PLUS   if treatgroup!="Control" & state==`sta', a(estrato#enumerador)  vce(cluster school_id)

	matrix tempm=e(b)
	local tempm=string(tempm[1,1], "%9.2gc")
	local tempm_effect=tempm[1,1]

	file open newfile using "$tables/`x'_coef_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	test PEC_PLUS
	if r(p)<0.001 {
		di "peque"
		local tempm ="$<0.001$"
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	if r(p)>0.001 {
		di "grande"
		local tempm=string(r(p), "%9.2gc")
		file open newfile using "$tables/`x'_pvalue_PEC_PLUS_vs_PEC_state_`sta'.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	
	
}

esttab using "$tables/state_`sta'_PEC_PLUSESTRA.tex" ,  fragment se ar2 nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none) ///
	keep(PEC_PLUS) coef(none) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	}
